import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule } from '@nestjs/swagger';
import { SwaggerConfig, multipartConfig } from './config';
import { ConfigService } from '@nestjs/config';
const path = require('node:path')

async function bootstrap() {
  // const app = await NestFactory.create(AppModule);
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
    // {logger:['error','warn','log']}
  );

  // 注册静态文件插件
  await app.register(require('@fastify/static'), {
    root: path.join(process.cwd(), 'public'),
    prefix: '/static/',
  });

  app.useGlobalPipes(new ValidationPipe({
    transform: true,  //类型转换
    // whitelist: true,  // 自动删除非 DTO 字段
    forbidNonWhitelisted: true // 可选：禁止非白名单字段，返回 400 错误
  }))

  // 配置 Swagger
  const document = SwaggerModule.createDocument(app, SwaggerConfig);
  if (process.env.NODE_ENV !== 'production') {
    SwaggerModule.setup('docs', app, document);
  }
    
  const configService = app.get(ConfigService);
  // 注册 文件上传插件
  await app.register(require('@fastify/multipart'),multipartConfig(configService));

  // 本地服务默认可能仅监听 127.0.0.1（localhost）,改为 0.0.0.0 允许外部 IP 访问
  await app.listen(process.env.PORT ?? 5000, '0.0.0.0');

}
bootstrap();
